package com.xd.hbsys.windmap;

import java.sql.Timestamp;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

@Service
public class WindmapService {

    @Resource(name = "hbJdbcTemplate")
    private JdbcTemplate hbJdbcTemplate;

    public String queryReal() {
        String sql = "SELECT time_BJ FROM windmap_realtime ORDER BY time_BJ DESC LIMIT 12 ";
        List<Map<String, Object>> rows = hbJdbcTemplate.queryForList(sql);
        Iterator it = rows.iterator();
        JSONArray arr = new JSONArray();
        while (it.hasNext()) {
            Map<String, Object> r = (Map<String, Object>) it.next();
            /*Timestamp ts = (Timestamp) r.get("time_BJ");*/
            /*String timecode = ts.toString();*/
            String timecode = r.get("time_BJ").toString();
            arr.add(timecode);
        }
        return arr.toJSONString();
    }

    public String queryFore() {
        String sql = "SELECT time_BJ FROM windmap_forecast ORDER BY time_BJ DESC LIMIT 12";
        List<Map<String, Object>> rows = hbJdbcTemplate.queryForList(sql);
        Iterator it = rows.iterator();
        JSONArray arr = new JSONArray();
        while (it.hasNext()) {
            Map<String, Object> r = (Map<String, Object>) it.next();
           /* Timestamp ts = (Timestamp) r.get("time_BJ");
            String timecode = ts.toString();*/
            String timecode = r.get("time_BJ").toString();
            arr.add(timecode);
        }
        return arr.toJSONString();
    }

    public String queryReal(String time) {
        String sql = "SELECT * FROM windmap_realtime where time_BJ = '" + time + "'";
        List<Map<String, Object>> rows = hbJdbcTemplate.queryForList(sql);
        Iterator it = rows.iterator();
        if (it.hasNext()) {
            Map<String, Object> r = (Map<String, Object>) it.next();
            String json = (String) r.get("json_data");
            return json;
        }
        return "[]";
    }

    public String queryFore(String time) {
        String sql = "SELECT * FROM windmap_forecast where time_BJ = '" + time + "'";
        List<Map<String, Object>> rows = hbJdbcTemplate.queryForList(sql);
        Iterator it = rows.iterator();
        if (it.hasNext()) {
            Map<String, Object> r = (Map<String, Object>) it.next();
            String json = (String) r.get("json_data");
            return json;
        }
        return "[]";
    }

}
